package vg.services.user_interface_manager.additional_swing_components.conditionPanel.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Set;

import javax.swing.*;

import vg.services.user_interface_manager.additional_swing_components.Bracket;

public class StringCondition implements ICondition {
	// Main data
	private Set<String> stringAttributeSet;

	// Swing data
	private JComboBox attributeNameComboBox; // left part of conditional
	private JTextField valueTextField; // right part of conditional

    private JButton closeButton; // button for closing (deleting) of condition

    private Bracket leftBracketBox;
	private Bracket rightBracketBox;

    private JPanel view;

	public StringCondition(Set<String> stringAttributeSet) {
		// initialize main data
		this.stringAttributeSet = stringAttributeSet;

        // initialize swing components
        view = new JPanel(new GridBagLayout());
		
		String[] attributeNames = new String[stringAttributeSet.size()];
		attributeNames = stringAttributeSet.toArray(attributeNames);
		Arrays.sort(attributeNames);
		attributeNameComboBox = new JComboBox(attributeNames);

    	valueTextField = new JTextField();
		
		// initialize close button
		// TODO: need change text to picture
		closeButton = new JButton("Close");
		
		// initialize brackets
		leftBracketBox = new Bracket(Bracket.DEF_LEFT_BRACKET);
		rightBracketBox = new Bracket(Bracket.DEF_RIGHT_BRACKET);

		// package user interface
		view.add(closeButton, new GridBagConstraints(0,0,  1,1,  0,0,  GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));
		view.add(leftBracketBox.getView(), new GridBagConstraints(0,1,  1,1,  0,0,  GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));
		view.add(attributeNameComboBox, new GridBagConstraints(1,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));
		view.add(valueTextField, new GridBagConstraints(2,1,  1,1,  1,0,  GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(3, 3, 3, 3),  0,0));
		view.add(rightBracketBox.getView(), new GridBagConstraints(3,1,  1,1,  0,0,  GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));
	}

	public String getName() {
		Object name = attributeNameComboBox.getSelectedItem();
		if(name == null)
			 return "";
		return name.toString();
	}
	
	public String getValue() {
		return valueTextField.getText();
	}
	
	public String getLeftBracket() {
		return leftBracketBox.getText();
	}
	
	public String getRightBracket() {
		return rightBracketBox.getText();
	}
	
	@Override
	public JComponent getView() {
		return view;
	}
	
	@Override
	public void updateUITheme() {
		view.updateUI();
	}

	@Override
	public void addCloseListener(ActionListener l) {
		closeButton.addActionListener(l);
	}	
	
	@Override
	public void removeCloseListener(ActionListener l) {
		closeButton.removeActionListener(l);
	}
}
